import { AsyncAction } from "../../internal/scheduler/AsyncAction";
import { creatorIntervalProvider } from "./creatorIntervalProvider";
import { CreatorScheduler } from "./CreatorScheduler";

export class CreatorAction<T> extends AsyncAction<T> {

    protected requestAsyncId(scheduler: CreatorScheduler, _id?: any, delay: number = 0): any {
        return creatorIntervalProvider.setInterval(scheduler.flush.bind(scheduler, this), delay, scheduler.node);
    }

    protected recycleAsyncId(_scheduler: CreatorScheduler, id: any, delay: number | null = 0): any {
        if (delay != null && this.delay === delay && this.pending === false) {
            return id;
        }
        creatorIntervalProvider.clearInterval(id, _scheduler.node);
        return undefined;
    }
}